package vn.team.clipxonxao.ui;

import java.util.List;

import vn.team.clipxonxao.adapter.SlidingMenuAdapter;
import vn.team.clipxonxao.model.Category;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.mk.fw.imageloader.MKImageFetcher;
import com.mk.fw.ui.MKFragment;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class SlidingMenuFragment extends MKFragment{

	private View mView;
	private ListView mListView;
	private List<Category> listCategory;
	private FragmentActivity mContext;
	private SlidingMenuAdapter adapter;
	private MKImageFetcher imageLoader;
	
	public SlidingMenuFragment(){
	}
	
	public SlidingMenuFragment(List<Category> listCategory){
		this.listCategory = listCategory;
	}
	
	private void initilView(View view){
		mListView = (ListView)view.findViewById(R.id.layout_slidingmenu_fragment_listCategoryMenu);
		mContext = getActivity();
		imageLoader = new MKImageFetcher(mContext, getFragmentManager(), 60, 45);
		adapter = new SlidingMenuAdapter(mContext, listCategory , imageLoader);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View viewgroup, int position,
					long id) {
				switchFragment(new HomeClipXonXao(listCategory.get(position).getId(),listCategory.get(position).getName()));
			}
		});
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.layout_slidingmenu_fragment, null, false);
		initilView(mView);
		return mView;
	}
	
	private void switchFragment(MKFragment fragment) {
		if (getActivity() == null)
			return;
		getActivity().getSupportFragmentManager().beginTransaction()
				.replace(R.id.layout_main_content_home, fragment).commit();
		Handler h = new Handler();
		h.postDelayed(new Runnable() {
			public void run() {
				((SlidingFragmentActivity) getActivity()).getSlidingMenu()
						.showContent();
			}
		}, 50);
	}
}
